const { mfutil } = getApp();
let { HttpRequest, globalData } = getApp()
const params = {}
Component({
  mixins: [],
  data: {
    dataArray: null
  },
  props: {
    json: ''
  },
  didMount() {
    console.log(this.props.tileActive);
    
    let { json, type, category, islucky, requiredduration, todo, activetype } = this.props;
    // this.setData({
    //   json, type, category, islucky, requiredduration, todo, activetype
    // })
    console.log(json);
    
    if (json != '') {
      this.init();
    }
    // this.init();
  },
  didUpdate() {
    let { json, type, category, islucky, requiredduration, todo, activetype, tileActive } = this.props;
     
    if (json && type && category && islucky && requiredduration && todo && activetype && tileActive) {
      return
    } else {
  
      this.setData({
        json, type, category, islucky, requiredduration, todo, activetype, tileActive
      })
      if (json != '') {
        this.init();
      }
      // this.init();
    }
  },
  didUnmount() { },
  methods: {
    winChange() {
    },
    async init() {
      console.log(123123123);
      
      let { json, type, category, islucky, requiredduration, todo, activetype } = this.props;
      // let { type, json, category, requiredduration, todo, activetype, } = this.data;
      let path = '/vans-family/api/v2.1/project/vansFamily/cms/tilesViewTemplate/search'
      let parmasObj = {
        tilesViewTemplate: {
          type: type
        }
      }
      let res = await HttpRequest(path, 'POST', '', parmasObj)
      let dataArray = null;
      let { tileActive } = this.data;
      if (islucky) {
        islucky = JSON.parse(islucky);
      }
      
      if (res.data.aaData) {
        console.log(res.data);
        
        if ((type == 'exclusive_sweepstakes' || type == 'reward_sweeps') && tileActive === 'finished') {
          
          // console.log(tileActive, 'tileActivetileActivetileActive')
          if (!json.is_come_out) {
            dataArray = res.data.aaData[3].extensionParams.json.data;
            console.log('未开奖')
          } else {
            if (!islucky) {
              dataArray = res.data.aaData[2].extensionParams.json.data;
              console.log('未中奖')
            } else {
              dataArray = res.data.aaData[1].extensionParams.json.data;
              console.log('已中奖')
            }
          }
        } else if (!tileActive) {
          dataArray = res.data.aaData[0].extensionParams.json.data;
        }

        //数据匹配d
        
        let time = json.start_time ? json.start_time.replace('-', '年').replace('-', '月').replace(' ', '日') : '';
        dataArray = dataArray.map((item, i) => {
          
          let pointstype = json.points_type == '1' ? '+' : json.points_type == '-1' ? "-" : '';
          switch (item.name1) {
            case "title": item.text = json.title
              break;
            case "point": item.text = pointstype + json.points + '积分'
              break;
            case "pointj": item.text = pointstype + json.points + '积分'
              break;
            case "image": item.src = json.cover_image
              break;
            case "desc": item.text = json.intro
              break;
            case "progress": item.points_user = globalData.userInfo.availablePoint, item.points = json.points
              break;
            case "datawraing": item.text = `中奖结果将于${time}揭晓,后续修改`
            // break;
            case "video": item.json = json,item.activetype = activetype, item.src = json.video, item.requiredduration = requiredduration, item.tileid = json.id, item.tiletyle = json.type, item.poster = json.cover_image, item.todo = todo
              break;
            case "unluck": item.text = json.unlucky_text;
              break;
            case "luck": item.text = json.lucky_text;
              break;
            case "winChange": item.text = json.complete_text + "";
              break;
          }
          return item
        })
        this.setData({
          dataArray: JSON.stringify(dataArray),
          a: true
        })
        console.log(this.data.dataArray  +'=======================================');
        
        
      }
    }
  },
});




